# Maintainer: Antonio Rojas <arojas@archlinux.org>
# Contributor: Ronald van Haren <ronald.archlinux.org>
# Contributor: damir <damir@archlinux.org>
# Contributor: Tom Newsom <Jeepster@gmx.co.uk>

pkgbase=gnuplot
pkgname=(gnuplot
         gnuplot-demos)
pkgver=5.4.10
pkgrel=1
pkgdesc='Plotting package which outputs to X11, PostScript, PNG, GIF, and others' 
arch=(loong64 x86_64)
url='http://www.gnuplot.info'
license=(custom)
depends=(cairo
         gcc-libs
         gd
         glib2
         glibc
         gtk3
         libcaca
         libcerf
         libx11
         lua
         pango
         qt5-base
         qt5-svg
         readline
         wxwidgets-common
         wxwidgets-gtk3) 
makedepends=(emacs
             qt5-tools
             texlive-latexextra)
source=(https://downloads.sourceforge.net/sourceforge/$pkgname/$pkgname-$pkgver.tar.gz
        lua53_compat.patch)
sha256sums=('975d8c1cc2c41c7cedc4e323aff035d977feb9a97f0296dd2a8a66d197a5b27c'
            'bfd8a61abbf4491c74225cb9fd252619d4fc29751838bcb4c0639ffe05a00695')

prepare() {
  cd $pkgname-$pkgver

  # fix default source location; use the GDFONTPATH variable to modify at runtime
  sed -i 's|/usr/X11R6/lib/X11/fonts/truetype|/usr/share/fonts/TTF|' src/variable.c

  sed -i -e 's|/usr/X11R6/lib/X11/fonts/Type1|/usr/share/fonts/Type1|' \
  -e 's|$(X11ROOT)/X11R6/lib/X11/fonts/Type1|$(X11ROOT)/usr/share/fonts/Type1|' \
  src/variable.c

  patch -p1 < "$srcdir"/lua53_compat.patch
}

build() {
  cd $pkgbase-$pkgver

  MAKEINFO=/usr/bin/makeinfo \
  ./configure --prefix=/usr \
              --libexecdir=/usr/bin \
              --with-gihdir=/usr/share/gnuplot \
              --with-readline=gnu \
              --with-bitmap-terminals \
              --with-wx-single-threaded \
              --with-caca \
              --with-texdir=/usr/share/texmf/tex/latex/gnuplot
  make pkglibexecdir=/usr/bin
}

package_gnuplot() {
  cd $pkgbase-$pkgver
  make pkglibexecdir=/usr/bin DESTDIR="$pkgdir" install
  make DESTDIR="$pkgdir" install -C demo

  install -Dm644 Copyright -t "$pkgdir"/usr/share/licenses/$pkgname

  rm -f "$pkgdir"/usr/share/texmf-dist/ls-R
}

package_gnuplot-demos() {
  pkgdesc='Demos for gnuplot'
  depends=(gnuplot)

  install -Dm644 $pkgbase-$pkgver/demo/*.{dem,dat} -t "$pkgdir"/usr/share/gnuplot/demos
}
